package com.example.myapplication.adapter;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.Lifecycle;
import androidx.viewpager2.adapter.FragmentStateAdapter;

import com.example.myapplication.fragment.PreviewFragment;

import java.util.List;

/**
 * created by moxun on 5/20/21
 * moxun@zhuangdianwenhua.com
 */
public class ImageDetailAdapter extends FragmentStateAdapter {

    private List<String> data;

    private int startPosition;

    public ImageDetailAdapter(@NonNull FragmentActivity fragmentActivity, List<String> data,int startPosition) {
        super(fragmentActivity);
        this.data = data;
        this.startPosition=startPosition;
    }

    public ImageDetailAdapter(@NonNull Fragment fragment, List<String> data,int startPosition) {
        super(fragment);
        this.data = data;
        this.startPosition=startPosition;
    }

    public ImageDetailAdapter(@NonNull FragmentManager fragmentManager,
                              @NonNull Lifecycle lifecycle, List<String> data,int startPosition) {
        super(fragmentManager, lifecycle);
        this.data = data;
        this.startPosition=startPosition;
    }

    public Fragment getFragment(FragmentManager fragmentManager, int position) {
        if (fragmentManager != null) {
            return fragmentManager.findFragmentByTag("f" + getItemId(position));
        }
        return null;
    }

    @NonNull
    @Override
    public Fragment createFragment(int position) {
        String model = data.get(position);

        Fragment currentFragment = PreviewFragment.newInstance(data.get(position), position,startPosition);

        return currentFragment;
    }

    @Override
    public int getItemCount() {
        return data != null ? data.size() : 0;
    }


}
